//
//  WGTestHeartRateViewController.m
//  QuickCure1
//
//  Created by Apple on 16/11/2.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "WGTestHeartRateViewController.h"
#import "HMDrawView.h"

@interface WGTestHeartRateViewController ()<CAAnimationDelegate>

@property(nonatomic,weak)CALayer *layer;

/** 绘制View */
@property (weak, nonatomic) IBOutlet HMDrawView *v_draw;

/**显示心率 */
@property (weak, nonatomic) IBOutlet UILabel *lb_heartRate;

/* 定时器 */
@property(nonatomic,strong)NSTimer *timer;

/* 心率 */
@property(nonatomic,assign)NSInteger heartRate;

/**开始按钮*/

@property(nonatomic,weak) UIButton *startButton;

/** 计算角度*/

@property(nonatomic,assign)CGFloat angle;

@end

@implementation WGTestHeartRateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置界面
    [self setupUI];
    
}

#pragma
#pragma mark -  搭建UI界面
-(void)setupUI{
    
    //1.创建图层
    
    CALayer *layer = [CALayer layer];
    //关联
    _layer = layer;
    
    layer.bounds = CGRectMake(0, 0, 100, 100);
    layer.position = self.view.center;
    layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"xyg"].CGImage);
    
    //角半径
    layer.cornerRadius = 50;
    layer.masksToBounds = YES;
    
    [self.view.layer addSublayer:layer];
    
    
    //2.创建按钮
    UIButton *startButton = [UIButton buttonWithType:0];
    _startButton = startButton;
    
    [startButton setBackgroundImage:[UIImage imageNamed:@"startButton"] forState:UIControlStateNormal];
    
    startButton.layer.cornerRadius = 40;
    startButton.layer.masksToBounds = YES;
    
    [self.view addSubview:startButton];
    
    
    [startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(150);
        make.width.height.mas_equalTo(80);
        
    }];
    
    
    
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    
}



#pragma
#pragma mark - 开始动画化
-(void)start{
    
    
    static int  num = 1;
    
    if (num == 1) {
        
        //1.创建动画
        
        CABasicAnimation *am = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        
        //2.设置属性
        
        am.toValue = @(2);
        am.repeatCount = 80;
        am.duration = 1;
        
        am.delegate = self;
        
        //添加
        [_layer addAnimation:am forKey:@"12"];
        num = 0;
        
    }else if(num == 0){
        
        [_layer removeAnimationForKey:@"12"];
        
        [_timer invalidate];
        
        _timer = nil;
        
        num = 1;
    }

}

-(void)dispalyTime{
    
//    static float i = 0 ;
    
    self.v_draw.endAngle = self.angle ;
    self.angle += 0.0126;
    self.lb_heartRate.text = [NSString stringWithFormat:@"心跳 %zd 次",self.heartRate++  ];
    
    
    NSLog(@">>>>%f", self.angle);
    
}

#pragma
#pragma mark - 动画的代理方法
- (void)animationDidStart:(CAAnimation *)anim{
    
    [[NSOperationQueue mainQueue]addOperationWithBlock:^{
        
        NSTimer *timer = [NSTimer timerWithTimeInterval: 1 target:self selector:@selector(dispalyTime) userInfo:nil repeats:YES];
        _timer = timer;
        [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
     
    }];

       self.startButton.enabled = NO;
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    self.startButton.enabled = YES;
    [self.timer invalidate];
    self.timer = nil;
}

#pragma
#pragma mark - 视图将要消失的时候,让 self.angle = 0
-(void)viewWillDisappear:(BOOL)animated{
    
    //  self.angle = 0;
    [self.timer invalidate];
    self.timer = nil;
}
    

-(void)dealloc{
    
    NSLog(@"定时器销毁");
}

@end
